<template>
    <div>
        <h1>投资项目添加</h1>
        <table class="table table-bordered table-hover table-striped" >
        <tr>
            <td>投资项目名称 </td>
            <td><input type="text" v-model="I_Name" placeholder="请输入投资项目名称"/></td>
        </tr>
        <tr>
            <td>投资总金额 </td>
            <td><input type="text" v-model="I_Budget" placeholder="请输入投资总金额"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <button @click="add">添加</button>
            </td>           
        </tr>

    </table>

    </div>

</template>
<script setup lang="ts">
import axios from "axios";
import {ref} from "vue";
import { useRouter } from 'vue-router';

const router = useRouter();

const I_Name = ref("");
const I_Budget = ref(0);

const add = () => {
    if (I_Name.value == "" ) {
        alert("请输入投资项目名称");
        return;
    }

    //投资总金额大于0小于1000000
    if (Number(I_Budget.value <= 0) || Number(I_Budget.value > 1000000)) {
        alert("投资总金额必须大于0小于1000000");
        return;
    }

    axios({
        method: "post",
        url: "http://localhost:57969/api/Invest/AddInvestInfo",
        data: {
            I_Name: I_Name.value,
            I_Budget: I_Budget.value
        }

    })
    .then(res => {
        if(res.data>0)
        {
            alert("添加成功");
            router.push("/user/InvestShow");
        }
        else if(res.data==-1)
        {
            alert("投资项目名称已存在");
            return;
        }
        else
        {
            alert("添加失败");
        }
            
    })

    
        
}
</script>